﻿using Basic.FormCollect;
using Basic.FormLocalEvent.Model;
using Basic.FormModel.DB;
using Basic.FormModel.PageLayout;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.PageLayout.Chart;
using Basic.FormRemoteModel.PageLayout.Model;
using Basic.FormService.Interface;
using LinqKit;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;

namespace Basic.FormService.lmpl
{
    internal class PageLayoutService : IPageLayoutService
    {
        private readonly ICustomPageLayoutCollect _Service;
        private readonly IPageControlCollect _Control;

        public PageLayoutService ( ICustomPageLayoutCollect service, IPageControlCollect control )
        {
            this._Service = service;
            this._Control = control;
        }
        public void SaveSort ( Dictionary<long, int> sort )
        {
            this._Service.SaveSort(sort);
        }
        public PageLayoutForm[] GetForm ( long pageId )
        {
            PageLayoutItemDto[] list = this._Service.GetFormItem<PageLayoutItemDto>(pageId);
            if ( list.IsNull() )
            {
                return null;
            }
            return list.Convert(a =>
            {
                if ( a.ControlConfig == null )
                {
                    return null;
                }
                long[] formId = null;
                if ( a.ControlType == PageControlType.指标图 )
                {
                    formId = a.ControlConfig.Statistics != null && a.ControlConfig.Statistics.FormId.HasValue ? new long[] { a.ControlConfig.Statistics.FormId.Value } : null;
                }
                else if ( a.ControlType == PageControlType.图表 )
                {
                    formId = a.ControlConfig.Chart != null && a.ControlConfig.Chart.FormId.HasValue ? new long[] { a.ControlConfig.Chart.FormId.Value } : null;
                }
                else if ( a.ControlType == PageControlType.明细表 )
                {
                    formId = a.ControlConfig.Table != null && a.ControlConfig.Table.FormId != 0 ? new long[] { a.ControlConfig.Table.FormId } : null;
                }
                else if ( a.ControlType == PageControlType.统计表 )
                {
                    formId = a.ControlConfig.CensusTable != null && a.ControlConfig.CensusTable.FormId != 0 ? new long[] { a.ControlConfig.CensusTable.FormId } : null;
                }
                if ( formId.IsNull() )
                {
                    return null;
                }
                return new PageLayoutForm
                {
                    Id = a.Id,
                    Name = a.Name,
                    FormId = formId
                };
            });
        }
        public PageLayoutData Get ( long id )
        {
            DBCustomPageLayout layout = this._Service.Get(id);
            return layout.ConvertMap<DBCustomPageLayout, PageLayoutData>();
        }
        public bool Set ( long id, PageLayoutSet set )
        {
            DBCustomPageLayout layout = this._Service.Get(id);
            if ( layout.ControlType == FormRemoteModel.PageControlType.图表 )
            {
                PageChartSelect[] list = set.ControlConfig.Chart.Selects;
                int num = 1;
                list.ForEach(c =>
                {
                    if ( c.Alias.IsNull() )
                    {
                        string alias = Tools.ToFirstPinYin(c.Label);
                        if ( list.IsExists(a => a.Alias == alias) )
                        {
                            alias += num;
                            num += 1;
                        }
                        c.Alias = alias;
                    }
                });
            }
            return this._Service.Set(layout, set);
        }
        public void Delete ( long id )
        {
            DBCustomPageLayout sour = this._Service.Get(id);
            this._Service.Delete(sour);
            new PageLayoutEvent(sour).AsyncSend("Delete");
        }
        public PageLayoutBase Add ( PageLayoutAdd add )
        {
            DBPageControl control = this._Control.Get<DBPageControl>(add.ControlId);
            PageLayoutAddDto dto = new PageLayoutAddDto
            {
                Code = control.Code,
                ControlId = add.ControlId,
                ParentId = add.ParentId,
                ControlType = control.ControlType,
                HeightGrid = control.MinHeight,
                WidthRatio = control.MinWidth,
                Name = control.Name,
                PageId = add.PageId,
                Sort = add.Sort
            };
            long id = this._Service.Add(dto);
            return new PageLayoutBase
            {
                Classify = control.Classify,
                ControlPath = control.EditControlPath,
                HeightGrid = dto.HeightGrid,
                WidthRatio = dto.WidthRatio,
                ControlType = dto.ControlType,
                Code = dto.Code,
                Id = id,
                Name = dto.Name
            };
        }

        public void Move ( long id, long parentId )
        {
            DBCustomPageLayout layout = this._Service.Get(id);
            _ = this._Service.Move(layout, parentId);
        }
    }
}
